#include <iostream>
#include "quickcalculation.h"
#include "createtable.h"
#include "struct.h"
#include <iomanip>

using namespace std;

/**
* \author Umarin Hendrikse
* \date 18-12-2012
* \brief This is the quetelet index application. the application functions as a calculator specified for the quetelet index.
* \brief This is the quetelet index application. the application functions as a calculator specified for the quetelet index.
* \brief The application will give an indication if the person is healthy weight, underweight or overweight. it also features an option to create tables.
* \brief This application is meant for adults, lenght from 1.50 meters to 2.20 meters. must weight from 40 kilograms to 160 kilograms.
*/

string sName; //Name declaration with a maximum array size
struct UserData data; //sruct declaration

/**
* \brief This functions as a suboption menu after choosing option 2 in the main menu
* \brief Option 1: create table with manual unput
* \brief Option 2: create a table by opening an existing data file
* \brief Option 3: return to main menu
* \param iStepsSize = Intiger representing the weight steps in kilograms
* \param iEnd_Weight = Intiger representing the last weight value to calculate to
* \param cChoice = Character represeneting the choice chosen in the suboption menu
*/
void SubOptions()
{
    int iStepSize = 0;
    int iEnd_Weight = 0;
    char cChoice = '0';

    CreateTable *MakeTable = new CreateTable();

    while(cChoice == '0')
    {
        cout << endl << "===============================================================================";
        cout << endl << "===============================================================================" << endl << endl << endl;
        cout << endl << "This is the sub menu of the Quetelet-index application." << endl;
        cout << endl << "[Option 1] Create a table with manual input" << endl;
        cout << endl << "[Option 2] Create a table by opening an existing data file" << endl;
        cout << endl << "[Option 3] Return to main menu" << endl;

        cout << endl << "Please insert the option of choice: ";
        do
        {
            cin >> cChoice;
        }
        while(cChoice == '\n');

        switch(cChoice)
        {
            case '1':
                MakeTable->GetName(sName);
                MakeTable->InputUserData(data);
                if(!MakeTable->InputWeightSteps(iStepSize, iEnd_Weight, data.iweight))
                    return;
                MakeTable->CreateQueteletIndexTable(sName, data, iStepSize, iEnd_Weight);
                break;
            case '2':
                //GetName(Name);
                if(MakeTable->InputUserDataFromFile(data, sName) == 0)
                {
                    break;
                }
                if(!MakeTable->InputWeightSteps(iStepSize, iEnd_Weight, data.iweight))
                    return;
                MakeTable->CreateQueteletIndexTable(sName, data, iStepSize, iEnd_Weight);
                break;
            case '3':
                break;
            default:
                cChoice = '0';
                cout << endl << "[ERROR:INPUT NOT  ACCEPTED BECAUSE THE INPUT IS INCORRECT!]";
                cout << endl << "\n[ERROR: PLEASE TRY AGAIN]"<< endl << endl << endl;
                break;
        }
    }
    delete MakeTable;
}

/**
* \brief This is the starting menu when opening the application. the application asks the user to choose 1 of the 3 options that are displayed
* \brief Option 1: quick quetelet-index calculation
* \brief Option 2: create a table with multiple calculations
* \brief Option 3: quit
* \return 0 to exit the application without errors
*/
int main()
{
    std::cout << std::setprecision(2) << std::fixed;
    char cMainOptionChoice; //Functions as character representing the the main menu option of choice.
    int iQuit = 1; //Functions as an Integer representing the option to quit the application.
    float fQresult;
    string sQueteletIndex;
    QuickCalculation *QuickCalc = new QuickCalculation();

    while (iQuit != 0)
    {


    printf("\n\t\t\t\t     MENU\n");
    cout << endl << "===============================================================================";
    cout << endl << "===============================================================================" << endl;
    cout << endl << "Welcome to the main menu of the Quetelet-index application." << endl;
    cout << endl << "[Option 1] Quick Quetelet-index calculation" << endl;
    cout << endl << "[Option 2] Create a table with multiple calculations" << endl;
    cout << endl << "[Option 3] Quit" << endl;

    cout << endl << "Please insert the option of choice: ";
    do
    {
        cin >> cMainOptionChoice;
    }
    while(cMainOptionChoice == '\n');

        switch (cMainOptionChoice)
        {
            case '1':
                QuickCalc->GetName(sName);
                QuickCalc->InputUserData(data);
                fQresult = QuickCalc->QueteletIndexCalculation(data);
                QuickCalc->ConvertQueteletIndexToString(fQresult, sQueteletIndex);
                cout << endl << "Queteletindex: " << fQresult;
                cout << endl << "Queteletindex: " << sQueteletIndex << endl << endl;
                system("pause"); //press any key function
                break;
            case '2':
                SubOptions();
                break;
            case '3':
                iQuit = 0;
                break;
            default:
                cout << endl << "[ERROR: INPUT NOT ACCEPTED BECAUSE THE INPUT IS INCORRECT!]";
                cout << endl << "[ERROR: PLEASE TRY AGAIN]" << endl << endl << endl;
                break;
        }
    }
    delete QuickCalc;
    return 0;
}

